const image = require('../../../lib/data/smoke.png');

var viewModel = {
  emissionRate: 5.0,
  gravity: 0.0,
  minimumParticleLife: 20,
  maximumParticleLife: 50,
  minimumSpeed: 1.0,
  maximumSpeed: 4.0,
  startScale: 1,
  endScale: 5.0,
  particleSize: 50.0,
  lifetime: 20,
};

export function volcano_burst(Cesium, viewer, source) {

  var planePosition = source;
  var particlesOffset = new Cesium.Cartesian3();
  var cameraLocation = Cesium.Cartesian3.add(planePosition, particlesOffset, new Cesium.Cartesian3());

  // Add plane to scene
  var hpr = new Cesium.HeadingPitchRoll(0.0, Cesium.Math.PI_OVER_TWO, 0.0);
  var orientation = Cesium.Transforms.headingPitchRollQuaternion(planePosition, hpr);
  var entity = viewer.entities.add({
    position: planePosition,
    orientation: orientation
  });

  // creating particles model matrix
  var translationOffset = Cesium.Matrix4.fromTranslation(particlesOffset, new Cesium.Matrix4());
  var translationOfPlane = Cesium.Matrix4.fromTranslation(planePosition, new Cesium.Matrix4());
  var modelMatrix = Cesium.Matrix4.multiplyTransformation(translationOfPlane, translationOffset, new Cesium
    .Matrix4());

  var scratchCartesian3 = new Cesium.Cartesian3();
  var scratchCartographic = new Cesium.Cartographic();

  var forceFunction = function() {
    return function(particle, dt) {
      dt = Cesium.Math.clamp(dt, 0.0, 100);

      scratchCartesian3 = Cesium.Cartesian3.normalize(particle.position, new Cesium.Cartesian3());
      scratchCartesian3 = Cesium.Cartesian3.multiplyByScalar(scratchCartesian3, 40.0 * dt, scratchCartesian3);
      scratchCartesian3 = Cesium.Cartesian3.add(particle.position, scratchCartesian3, scratchCartesian3);
      scratchCartographic = Cesium.Cartographic.fromCartesian(scratchCartesian3, Cesium.Ellipsoid.WGS84,
        scratchCartographic);

      particle.position = Cesium.Cartographic.toCartesian(scratchCartographic);
    };
  };

  function createParticleSystems() {

    var emitterModelMatrix = Cesium.Matrix4.IDENTITY;

    var updateCallback = forceFunction();

    let particleSystem = new Cesium.ParticleSystem({
      image,
      startColor: Cesium.Color.RED.withAlpha(0.15),
      endColor: Cesium.Color.WHITE.withAlpha(0.0),
      // startColor: color,
      // endColor: color.withAlpha(0.50),
      // particleLife: 30, //决定喷射的高度
      minimumParticleLife: viewModel.minimumParticleLife,
      maximumParticleLife: viewModel.maximumParticleLife,

      startScale: viewModel.startScale,
      endScale: viewModel.endScale,

      // speed: 0.00005,
      minimumSpeed: viewModel.minimumSpeed,
      maximumSpeed: viewModel.maximumSpeed,

      // imageSize: imageSize,
      imageSize: new Cesium.Cartesian2(
        viewModel.particleSize,
        viewModel.particleSize
      ),

      // emissionRate: 1.0,
      emissionRate: viewModel.emissionRate,

      emitter: new Cesium.CircleEmitter(1),
      lifetime: viewModel.lifetime,

      bursts: [
        // these burst will occasionally sync to create a multicolored effect
        new Cesium.ParticleBurst({
          time: 5.0,
          minimum: 10,
          maximum: 100,
        }),
        new Cesium.ParticleBurst({
          time: 10.0,
          minimum: 50,
          maximum: 100,
        }),
        new Cesium.ParticleBurst({
          time: 15.0,
          minimum: 100,
          maximum: 200,
        }),
      ],

      updateCallback,
      modelMatrix,
      emitterModelMatrix
    })

    particleSystem = viewer.scene.primitives.add(particleSystem);

    return particleSystem
  }

  var particleSystem = createParticleSystems();

  var camera = viewer.scene.camera;
  console.log("source", source)
  console.log(camera._positionWC)
  console.log(camera.position)

  // var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  // handler.setInputAction((movement) => {
  //   console.log(camera)
  //   console.log(camera._positionWC)
  //   // console.log(movement)
  //   particleSystem.startScale = 0.1
  //   particleSystem.endScale = 2
  // }, Cesium.ScreenSpaceEventType.WHEEL);

  var distance0 = null;
  var cameraPostion0 = null;

  viewer.scene.camera.moveEnd.addEventListener(function() {
    if (!cameraPostion0) {
      cameraPostion0 = camera._positionWC.clone()
      distance0 = Cesium.Cartesian3.distance(
        source,
        camera._positionWC
      );
      console.log(distance0)
    }
    console.log(camera)
    console.log(camera._positionWC)
    console.log(camera.position)

    var distance = Cesium.Cartesian3.distance(
      planePosition,
      camera._positionWC
    );

    console.log(distance)
    
    var startScale = viewModel.startScale
    var endScale = viewModel.endScale
    
    var r = 1;// (distance0/distance)
    r = r>10?10:r;
    
    startScale = viewModel.startScale*r
    endScale = viewModel.endScale*r
    
    particleSystem.startScale = startScale
    particleSystem.endScale =  endScale
  })
}
